package com.ruoyi.business.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.business.BO.MatchContractInfoBO;
import com.ruoyi.business.domain.AcCustomerInfo;
import com.ruoyi.business.domain.AcLeaseInfo;
import com.ruoyi.business.mapper.AcCustomerInfoMapper;
import com.ruoyi.business.mapper.AcLeaseInfoMapper;
import com.ruoyi.business.tool.ContractInfoTool;
import com.ruoyi.business.tool.UserInfoTool;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.AcContractInfoMapper;
import com.ruoyi.business.domain.AcContractInfo;
import com.ruoyi.business.service.IAcContractInfoService;

/**
 * 合同台账Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-18
 */
@Service
public class AcContractInfoServiceImpl implements IAcContractInfoService
{
    @Autowired
    private AcContractInfoMapper acContractInfoMapper;

    @Autowired
    private AcLeaseInfoMapper acLeaseInfoMapper;
    @Autowired
    private UserInfoTool userInfoTool;

    @Autowired
    private ContractInfoTool contractInfoTool;

    @Autowired
    private AcCustomerInfoMapper acCustomerInfoMapper;
    /**
     * 查询合同台账
     *
     * @param contractId 合同台账主键
     * @return 合同台账
     */
    @Override
    public AcContractInfo selectAcContractInfoByContractId(Long contractId)
    {
        return acContractInfoMapper.selectAcContractInfoByContractId(contractId);
    }

    /**
     * 查询合同台账列表
     *
     * @param acContractInfo 合同台账
     * @return 合同台账
     */
    @Override
    public List<AcContractInfo> selectAcContractInfoList(AcContractInfo acContractInfo)
    {
        acContractInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID
        acContractInfo.setContractIsDelete(0);  // 设置删除状态
        acContractInfo.setDelFlag("0");  // 设置删除状态
        return acContractInfoMapper.selectAcContractInfoList(acContractInfo);
    }

    public List<AcContractInfo> selectAcContractInfoMonthList(AcContractInfo acContractInfo)
    {
        acContractInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID
        acContractInfo.setContractIsDelete(0);  // 设置删除状态
        acContractInfo.setDelFlag("0");  // 设置删除状态
        return acContractInfoMapper.selectAcContractInfoMonthList(acContractInfo);
    }
    public List<AcContractInfo> selectAcContractInfoSeasonList(AcContractInfo acContractInfo)
    {
        acContractInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID
        acContractInfo.setContractIsDelete(0);  // 设置删除状态
        acContractInfo.setDelFlag("0");  // 设置删除状态
        return acContractInfoMapper.selectAcContractInfoSeasonList(acContractInfo);
    }

    /**
     * 新增合同台账
     *
     * @param acContractInfo 合同台账
     * @return 结果
     */
    @Override
    public int insertAcContractInfo(AcContractInfo acContractInfo)
    {
        acContractInfo.setCreateTime(DateUtils.getNowDate());  // 设置创建时间
        MatchContractInfoBO matchContractInfoBO = new MatchContractInfoBO();
        matchContractInfoBO.setDeptId(userInfoTool.getUserDeptId());
        String contractCode = contractInfoTool.generateRandomNumber(matchContractInfoBO);
        acContractInfo.setContractCode(contractCode);  // 设置合同编号
        acContractInfo.setCreateBy(userInfoTool.getUserName());  // 设置创建人
        acContractInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID
        acContractInfo.setContractIsDelete(0);

        //        将合同信息插入资产台账数据库
        insterAcLeaseInfo(acContractInfo);
        //        更新租客租赁状态
        updateCustomerInfo(acContractInfo);
        return acContractInfoMapper.insertAcContractInfo(acContractInfo);
    }


//    将合同信息插入资产台账数据库
    public int insterAcLeaseInfo(AcContractInfo acContractInfo)
    {

        AcLeaseInfo acLeaseInfo = new AcLeaseInfo();
        acLeaseInfo.setCreateTime(DateUtils.getNowDate());  // 设置创建时间


        //        判断当前时间是否在合同有效期，有效期内将leaseStatus置为0，否则为1
        if (acContractInfo.getContractEndDate().compareTo(DateUtils.getNowDate()) > 0) {
            acContractInfo.setContractStatus("0");  // 设置租赁状态
        } else {
            acContractInfo.setContractStatus("1");  // 设置租赁状态
        }
        Random random = new Random();
        acLeaseInfo.setLeaseRent(acContractInfo.getContractRent());
        acLeaseInfo.setLeaseCode(String.valueOf(random.nextInt(90000000) + 10000000));
        acLeaseInfo.setLeaseStatus(acContractInfo.getContractStatus());  // 设置租赁状态
        acLeaseInfo.setLeaseIsDelete(0);  // 设置删除状态
        acLeaseInfo.setCreateBy(userInfoTool.getUserName());  // 设置创建人
        acLeaseInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID
        acLeaseInfo.setLeaseDate(DateUtils.getNowDate());
//    资产id获取资产数据，存储
        acLeaseInfo.setLeaseContractCode(acContractInfo.getContractCode());
        acLeaseInfo.setLeaseCustomerId(acContractInfo.getContractId());
        acLeaseInfo.setLeaseAssetId(acContractInfo.getContractAssetId());
        acLeaseInfo.setLeaseCustomerId(acContractInfo.getContractCustomerId());
        acLeaseInfo.setLeaseStartDate(acContractInfo.getContractStartDate());
        acLeaseInfo.setLeaseEndDate(acContractInfo.getContractEndDate());
//        acLeaseInfo.setLeaseStatus("0");  // 设置租赁状态
//        acLeaseInfo.setLeaseIsDelete(0);  // 设置删除状态
        acLeaseInfo.setCreateBy(userInfoTool.getUserName());  // 设置创建人
        acLeaseInfo.setDeptId(userInfoTool.getUserDeptId());  // 设置部门ID

        //合同类型
// 租金
//  承租方 联系人 联系电话
//        acLeaseInfo.setC
        return acLeaseInfoMapper.insertAcLeaseInfo(acLeaseInfo);
    }


    /**
     *
     *更新租客租赁状态
     *
     * */
    public int updateCustomerInfo(AcContractInfo acContractInfo){
        AcCustomerInfo acCustomerInfo = new AcCustomerInfo();

        AcCustomerInfo result = new AcCustomerInfo();
        acCustomerInfo.setCustomerId(acContractInfo.getContractCustomerId());

        result = acCustomerInfoMapper.selectAcCustomerInfoByCustomerId(acCustomerInfo.getCustomerId());

        /*租赁次数+1*/
        acCustomerInfo.setCustomerLeaseCount(result.getCustomerLeaseCount() + 1);
//        acCustomerInfo.setCustomerLeaseStatus(acContractInfo.getLeaseStatus());

        //        判断当前时间是否在合同有效期，有效期内将leaseStatus置为0，否则为1
        if (acContractInfo.getContractEndDate().compareTo(DateUtils.getNowDate()) > 0) {
            acCustomerInfo.setCustomerLeaseStatus("0");  // 设置租赁状态
        } else {
            acCustomerInfo.setCustomerLeaseStatus("1");  // 设置租赁状态
        }
        return acCustomerInfoMapper.updateAcCustomerInfo(acCustomerInfo);
    }

    /**
     * 修改合同台账
     *
     * @param acContractInfo 合同台账
     * @return 结果
     */
    @Override
    public int updateAcContractInfo(AcContractInfo acContractInfo)
    {
        acContractInfo.setUpdateTime(DateUtils.getNowDate());  // 设置修改时间
        acContractInfo.setUpdateBy(userInfoTool.getUserName());  // 设置修改人
        return acContractInfoMapper.updateAcContractInfo(acContractInfo);
    }

    /**
     * 批量删除合同台账
     *
     * @param contractIds 需要删除的合同台账主键
     * @return 结果
     */
    @Override
    public int deleteAcContractInfoByContractIds(Long[] contractIds)
    {
        return acContractInfoMapper.deleteAcContractInfoByContractIds(contractIds);
    }

    /**
     * 删除合同台账信息
     *
     * @param contractId 合同台账主键
     * @return 结果
     */
    @Override
    public int deleteAcContractInfoByContractId(Long contractId)
    {
        return acContractInfoMapper.deleteAcContractInfoByContractId(contractId);
    }
}
